package com.lh.job.task;

import com.lh.common.entity.admin.ActivityShopOne;
import com.lh.common.entity.admin.Period;
import com.lh.common.enums.ActivityStatusEnum;
import com.lh.common.enums.LotteryTypeStatus;
import com.lh.common.enums.PeriodStatusEnum;
import com.lh.job.dao.ActivityShopOneMapper;
import com.lh.job.dao.PeriodMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author FH
 * @ClassName: ActiveShopPeriodController
 * @Description: (生成活动期数)
 * @date: 2020-05-21 9:22
 */
@Component("activeShopPeriod")
@Slf4j
public class ActiveShopPeriodController implements ITask{

    @Autowired
    private ActivityShopOneMapper activityShopOneMapper;

    @Autowired
    private PeriodMapper periodMapper;

    @Override
    public void run(String params) {
        log.info(">>>>>>>>>>>>>>>>> 生成活动期数定时任务开始执行");
        List<ActivityShopOne> activityShopOnes = activityShopOneMapper.queryActivityShopList();
        for(ActivityShopOne shopOnes : activityShopOnes){
            Period period = periodMapper.queryPeriod(shopOnes.getAid());
            Date date = new Date();
            if(period == null){
                period = new Period();
                if(shopOnes.getActivityType().equals(LotteryTypeStatus.STARTNOW)){
                    period.setAid(shopOnes.getAid());
                    period.setActivityName(shopOnes.getActivityName());
                    period.setPeriod(1);
                    period.setPortionNum(shopOnes.getPortionNum());
                    period.setTotalPortion(shopOnes.getPortionNum());
                    period.setActivityCycle(shopOnes.getActivityCycle());
                    period.setActivityStartTime(null);
                    period.setLotteryTime(new Date(date.getTime()+shopOnes.getActivityCycle()*1000));
                    periodMapper.insertPeriod(period);
                    shopOnes.setActivityStartTime(null);
                    shopOnes.setActivityStatus(ActivityStatusEnum.UNDERWAY);
                    activityShopOneMapper.updateActivityShopOne(shopOnes);
                }else if(shopOnes.getActivityType().equals(LotteryTypeStatus.STARTTIME)){
                    String activityStartTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(shopOnes.getActivityStartTime());
                    String newDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
                    if(activityStartTime.compareTo(newDate) <= 0){
                        period.setAid(shopOnes.getAid());
                        period.setActivityName(shopOnes.getActivityName());
                        period.setPeriod(1);
                        period.setPortionNum(shopOnes.getPortionNum());
                        period.setTotalPortion(shopOnes.getPortionNum());
                        period.setActivityCycle(shopOnes.getActivityCycle());
                        period.setActivityStartTime(date);
                        period.setLotteryTime(new Date(date.getTime()+shopOnes.getActivityCycle()*1000));
                        periodMapper.insertPeriod(period);
                        shopOnes.setActivityStatus(ActivityStatusEnum.UNDERWAY);
                        shopOnes.setActivityStartTime(new Date(date.getTime()+shopOnes.getActivityCycle()*1000+10000));
                        activityShopOneMapper.updateActivityShopOne(shopOnes);
                    }
                }
            }else if(shopOnes.getActivityType().equals(LotteryTypeStatus.STARTNOW)&&shopOnes.getActivityStartTime() == null && PeriodStatusEnum.REVEALED.equals(period.getStatus())){
                try {
                    BigDecimal sumOrderPrice = periodMapper.sumOrderPrice(period.getPid());
                    period.setPeriod(period.getPeriod()+1);
                    period.setAid(shopOnes.getAid());
                    period.setActivityName(shopOnes.getActivityName());
                    period.setPortionNum(shopOnes.getPortionNum());
                    period.setTotalPortion(shopOnes.getPortionNum());
                    period.setActivityCycle(shopOnes.getActivityCycle());
                    period.setActivityStartTime(null);
                    period.setLotteryTime(new Date(date.getTime()+shopOnes.getActivityCycle()*1000));
                    periodMapper.insertPeriod(period);
                    shopOnes.setActivityStartTime(null);
                    shopOnes.setSumProfitAmount(sumOrderPrice);
                    shopOnes.setActivityStatus(ActivityStatusEnum.UNDERWAY);
                    activityShopOneMapper.updateActivityShopOne(shopOnes);
                }catch (Exception e){
                    log.error("活动Id:"+period.getAid() + " 期数:"+(period.getPeriod()+1)+" 已存在!");
                    e.printStackTrace();
                }
            }else if(shopOnes.getActivityType().equals(LotteryTypeStatus.STARTTIME)){
                String activityStartTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(shopOnes.getActivityStartTime());
                String newDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
                if(activityStartTime.compareTo(newDate) <= 0 && PeriodStatusEnum.REVEALED.equals(period.getStatus())){
                    try {
                        BigDecimal sumOrderPrice = periodMapper.sumOrderPrice(period.getPid());
                        period.setPeriod(period.getPeriod()+1);
                        period.setAid(shopOnes.getAid());
                        period.setActivityName(shopOnes.getActivityName());
                        period.setPortionNum(shopOnes.getPortionNum());
                        period.setTotalPortion(shopOnes.getPortionNum());
                        period.setActivityCycle(shopOnes.getActivityCycle());
                        period.setActivityStartTime(date);
                        period.setLotteryTime(new Date(date.getTime()+shopOnes.getActivityCycle()*1000));
                        periodMapper.insertPeriod(period);
                        shopOnes.setSumProfitAmount(sumOrderPrice);
                        shopOnes.setActivityStatus(ActivityStatusEnum.UNDERWAY);
                        shopOnes.setActivityStartTime(new Date(date.getTime()+shopOnes.getActivityCycle()*1000+10000));
                        activityShopOneMapper.updateActivityShopOne(shopOnes);
                    }catch (Exception e){
                        log.error("活动Id:"+period.getAid() + " 期数:"+(period.getPeriod()+1)+" 已存在!");
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
